package com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.adapter;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Space;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.StuInforEditFragment;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.aboutstuFragment;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.aboutstuFragmentDirections;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.itembean.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

    List<Student> studentList = new ArrayList<>();
    ImageButton ib_delete,ib_edit,ib_phone;
    AlertDialog.Builder alert_builder;
    int CurrentPosition;
    public StudentAdapter(List<Student> studentList) {
        this.studentList = studentList;
    }


    @NonNull
    @Override
    public StudentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_about_stu, parent, false);
        return new StudentAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StudentAdapter.ViewHolder holder, int position) {
        Student student = studentList.get(position);
        holder.tvName.setText(student.getName());
        holder.tvStudentId.setText("学号:" + student.getStudentId());
        holder.tvPhone.setText(student.getPhone());
        ib_delete = holder.itemView.findViewById(R.id.ib_delete);
        ib_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CurrentPosition = holder.getBindingAdapterPosition();
                alert_builder = new AlertDialog.Builder(v.getContext());
                alert_builder.setTitle("提示")
                        .setMessage("是否删除该学生？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            Log.d("Position!!!!!!!!!!!!!!!!!!!!!!!!", String.valueOf(CurrentPosition));
                            studentList.remove(CurrentPosition);
                            notifyDataSetChanged();

                        })
                        .setNegativeButton("取消", (dialog, which) -> {
                            // 取消操作
                        })
                        .show();
            }
        });
        ib_edit = holder.itemView.findViewById(R.id.ib_edit);
        ib_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
//                使用生成的 Directions 类创建 action 从aboutstuFragment 到StuInforEditFragment
                aboutstuFragmentDirections.ActionAboutstuFragmentToStuInforEditFragment action = aboutstuFragmentDirections.actionAboutstuFragmentToStuInforEditFragment();
                action.setName(student.getName());
                action.setId(student.getStudentId());
                action.setPhone(student.getPhone());
                action.setClassName(student.getClassName());
                navController.navigate(action);
            }
        });
        //点击按钮去拨号
        ib_phone = holder.itemView.findViewById(R.id.ib_phone);
        ib_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + student.getPhone()));
                v.getContext().startActivity(intent);
            }
        });
    }

    private void UpDataStudent() {

    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{

        TextView tvName, tvStudentId, tvPhone;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvStudentId = itemView.findViewById(R.id.tvStudentId);
            tvPhone = itemView.findViewById(R.id.tv_phone);
        }
    }
}
